package com.woniuxy.cloud.carl.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.cloud.carl.user.common.ContantsDate;
import com.woniuxy.cloud.carl.user.infra.model.User;
import com.woniuxy.cloud.carl.user.infra.mapper.UserMapper;
import com.woniuxy.cloud.carl.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.cloud.carl.user.service.param.GetUserByConditionServiceParam;
import com.woniuxy.cloud.carl.user.service.param.LoginServiceParam;
import com.woniuxy.cloud.carl.user.service.param.RegisterServiceParam;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author fjx
 * @since 2023-07-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper mapper;

    @Override
    public Long login(LoginServiceParam serviceParam) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if(serviceParam.getUserPhone()!=null){
            wrapper.eq("user_phone",serviceParam.getUserPhone());
        }
        if(serviceParam.getUserEmail()!=null){
            wrapper.eq("user_email",serviceParam.getUserEmail());
        }
        User u = this.getOne(wrapper);
        return u.getUserId();
    }


    @Override
    public void register(RegisterServiceParam param) {
        User user = BeanUtil.toBean(param, User.class);
        user.setAvatar("https://bucket-book-fjx.oss-cn-chengdu.aliyuncs.com/952d5554475f4295982398a05704e51d.jpg");
        user.setScore(0);
        user.setUserState(ContantsDate.USER_STATE_NORMAL);
        user.setVip(ContantsDate.USER_NOT_VIP);
        save(user);
    }

    @Override
    public Page<User> getByCondition(GetUserByConditionServiceParam param) {
        Page<User> page = new Page<>( param.getPageSize() == null ? 1 : param.getPageSize(),param.getPageNum() == null ? 3 : param.getPageNum());
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if(param.getUserEmail()!=null) wrapper.eq("user_email",param.getUserEmail());
        if(param.getUserName()!=null) wrapper.eq("user_name",param.getUserName());
        if(param.getUserPhone()!=null) wrapper.eq("user_phone",param.getUserPhone());
        if(param.getVip()!=null) wrapper.eq("vip",param.getVip());
        mapper.selectPage(page,wrapper);
        return page;
    }
}
